package com.share.shareplatform.sharegoods.controller;


import com.share.shareplatform.pojo.Brand;
import com.share.shareplatform.pojo.exception.LogicException;
import com.share.shareplatform.sharegoods.service.BrandService;
import com.share.shareplatform.util.Result;
import io.swagger.annotations.ApiOperation;
import org.hibernate.validator.constraints.Length;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.security.PermitAll;
import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/brand")
@Validated
public class BrandController{

    @Autowired
    private BrandService brandService;

    @ApiOperation(value="添加品牌", notes="必需字段：name")
    @PostMapping
    @PreAuthorize("hasAuthority('brandAdd')")
    public Result addOne(@RequestBody @Validated Brand brand) throws LogicException {
        Brand.setInit(brand);
        Brand oneBrand = brandService.findOneByName(brand.getName());
        if(oneBrand!=null){
            throw new LogicException("品牌名称已存在");
        }
        Long one = brandService.addOne(brand);

        if(one==0){
            return Result.getInstance(505,"添加失败");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="更新品牌", notes="必需字段：id,name")
    @PutMapping
    @PreAuthorize("hasAuthority('brandUpdate')")
    public Result updateOne(@RequestBody @Validated Brand brand) throws LogicException {
        if(brand.getId()==null){
            return Result.getInstance(505,"Id不能为空");
        }
        Brand oneBrand = brandService.findOneByName(brand.getName());
        if(oneBrand!=null){
            throw new LogicException("品牌名称已存在");
        }
        Integer flag = brandService.updateOne(brand);
        if(flag==0){
            throw new LogicException("修改失败");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="删除单个品牌，按id")
    @DeleteMapping("/{id}")
    @PreAuthorize("hasAuthority('brandDelete')")
    public Result deleteOneById(@PathVariable Long id) {
        Integer flag = brandService.deleteOneById(id);
        if(flag==0){
            return Result.getInstance(505,"删除失败,该品牌不存在");
        }
        return Result.getInstance(200,"success");
    }

    @ApiOperation(value="批量删除品牌，按id",notes = "其中ids为 'id-id-id' 这种类型的字符串")
    @DeleteMapping("/all/{ids}")
    @PreAuthorize("hasAuthority('brandDelete')")
    public Result deleteAllByIds(@PathVariable String ids) {
        String[] stringIds=ids.split("-");
        ArrayList<Long> arrayList=new ArrayList<>();
        for(String id:stringIds){
            arrayList.add(Long.parseLong(id));
        }
        Integer flag = brandService.deleteAllByIds(arrayList.toArray(new Long[arrayList.size()]));
        if(flag==0){
            return Result.getInstance(505,"删除失败");
        }
        return Result.getInstance(200,"删除了"+flag+"条数据");

    }

    @ApiOperation(value="搜索品牌，按name")
    @GetMapping("/name/{name}")
    @PermitAll
    public Result findOneByName( @PathVariable
                                     @NotBlank(message = "名称不能为空")
                                     @Length(max = 20,message = "名称太长") String name) {
        Brand brand=brandService.findOneByName(name);
        if(brand==null){
            return Result.getInstance(505,"not found");
        }

        return Result.getInstance(200,"success").put("brand",brand);
    }

    @ApiOperation(value="搜索品牌，按id")
    @GetMapping("/{id}")
    @PermitAll
    public Result findOneById(@PathVariable Long id) {
        Brand brand=brandService.findOneById(id);
        if(brand==null){
            return Result.getInstance(505,"not found");
        }

        return Result.getInstance(200,"success").put("brand",brand);
    }

    @ApiOperation(value="查询所有品牌")
    @GetMapping()
    @PermitAll
    public Result findAll() {
        List<Brand> list = brandService.findAll();
        return Result.getInstance(200,"success").put("list",list);
    }
}
